
import 'package:flutter/material.dart';
import 'package:flutter_trip/pages/travel_child_page.dart';

import '../dao/travel_tab_dao.dart';
import '../model/travel_tab_model.dart';

class TravelPage extends StatefulWidget {
  const TravelPage({super.key});

  @override
  State<TravelPage> createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin{
  late TravelTabModel travelTabModel;
  late TabController _tabController;
  List<TravelTab> tabs = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: 0, vsync: this);
    _loadTabData();
  }

  _loadTabData(){
    TraveltabDao.fetch().then((value){
      _tabController = TabController(length: value.tabs.length, vsync: this);
      setState(() {
        travelTabModel = value;
        tabs = value.tabs;
      });
    }).catchError((e){

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.only(top: 40),
        child: Column(
          children: [
            TabBar(
              controller: _tabController,
                isScrollable: true,
                labelColor: Colors.black,

                tabs: _tabs(),
            ),
            Expanded(
                child: TabBarView(
                  controller: _tabController,
                  children: _tabBarView(),
                )
            )
          ],
        ),
      )
    );
  }
  
  _tabs(){
    List<Tab> list = [];
      for (final value in tabs){
        list.add(Tab(text: value.labelName ?? "",));
      }
    return list;
  }

  _tabBarView() {
    return tabs.map((e) {
      return TravelChildPage(url: travelTabModel.url,
        params: travelTabModel.params,
        groupChannelCode: e.groupChannelCode,);
    }).toList();
  }
}